---
id: running-tracetest-with-azure-app-insights-collector
title: Node.js and Azure Application Insights with OpenTelemetry Collector
description: Quick start on how to configure a Node.js app with OpenTelemetry traces, Azure Application Insights as a trace data store, including the OpenTelemetry Collector, and Tracetest for enhancing your E2E and integration tests with trace-based testing.
hide_table_of_contents: false
keywords:
  - tracetest
  - trace-based testing
  - observability
  - distributed tracing
  - testing
  - azure functions
  - azure app insights
  - azure application insights
  - azure tracing
  - azure monitor
  - otel collector
  - opentelemetry collector
  - opentelemetry
image: https://res.cloudinary.com/djwdcmwdz/image/upload/v1698686403/docs/Blog_Thumbnail_14_rsvkmo.jpg
---

:::note
[Check out the source code on GitHub here.](https://github.com/kubeshop/tracetest/tree/main/examples/tracetest-azure-app-insights-collector)
:::

[Tracetest](https://tracetest.io/) is a testing tool based on [OpenTelemetry](https://opentelemetry.io/) that allows you to test your distributed application. It allows you to use data from distributed traces generated by OpenTelemetry to validate and assert if your application has the desired behavior defined by your test definitions.

[Azure Application Insights](https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview) is an extension of Azure Monitor and provides application performance monitoring (APM) features. APM tools are useful to monitor applications from development, through test, and into production in the following ways:

- Proactively understand how an application is performing.
- Reactively review application execution data to determine the cause of an incident.

[OpenTelemetry Collector Contrib](https://github.com/open-telemetry/opentelemetry-collector-contrib) - The official OpenTelemetry Distribution for packages outside of the core collector.

## Node.js App with Azure Application Insights, OpenTelemetry Collector and Tracetest

This is a simple quick start guide on how to configure a Node.js app to use instrumentation with traces and Tracetest for enhancing your E2E and integration tests with trace-based testing. The infrastructure will use Azure App Insights as the trace data store, the OpenTelemetry Collector to process and route the telemetry data and a Node.js app to generate it.

## Prerequisites

**Tracetest Account**:

- Sign up to [`app.tracetest.io`](https://app.tracetest.io) or follow the [get started](/getting-started/overview) docs.
- Have access to the environment's [agent API key](https://app.tracetest.io/retrieve-token).

**Azure Account**:

- Sign up to [Azure](https://azure.microsoft.com/en-us).
- Install [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli).
- Create an Application Insights app and get a [Instrumentation Key](https://learn.microsoft.com/en-us/azure/bot-service/bot-service-resources-app-insights-keys?view=azure-bot-service-4.0).

**Docker**: Have [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) installed on your machine.

## Run This Quckstart Example

The example below is provided as part of the Tracetest project. You can download and run the example by following these steps:

Clone the Tracetest project and go to the Azure Node.js Quickstart:

```bash
git clone https://github.com/kubeshop/tracetest
cd tracetest/examples/tracetest-azure-app-insights-collector
```

Follow these instructions to run the quick start:

1. Copy the `.env.template` file to `.env`.
2. Fill out the [TRACETEST_TOKEN and ENVIRONMENT_ID](https://app.tracetest.io/retrieve-token) details by editing your `.env` file.
3. Fill out the [APP_INSIGHTS_INSTRUMENTATION_STRING](https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string?tabs=dotnet5#find-your-connection-string) details by editing your `.env` file.
4. Run `docker compose run tracetest-run`.
5. Follow the links in the output to view the test results.

Follow along with the sections below for an in detail breakdown of what the example you just ran did and how it works.

## Project Structure

The project contains the [Tracetest Agent](/getting-started/install-agent), and a Node.js app.

The `docker-compose.yaml` file in the root directory of the quick start runs the Node.js app, Elasticsearch, Elastic APM server, OpenTelemetry Collector, and the [Tracetest Agent](/concepts/agent) setup.

## Configuring the Node.js App

The Node.js app is a simple Express app, contained in [the `app.js` file](https://github.com/kubeshop/tracetest/blob/main/examples/tracetest-azure-app-insights-collector/src/app.js).

Configure the `.env` like shown below.

```bash
TRACETEST_API_KEY="<YOUR_TRACETEST_API_KEY>"
```

The OpenTelemetry tracing is contained in the `tracing.js` file. Traces will be sent to Azure Application Insights.

[The `tracing.js` file](https://github.com/kubeshop/tracetest/blob/main/examples/tracetest-azure-app-insights-collector/src/tracing.js) will send traces to the Azure Application Insights endpoint.

Enabling the tracer is done by preloading the trace file. As seen in the `package.json`.

```json
"scripts": {
  "start": "node -r ./tracing.js app.js"
},
```

## Configuring Azure Application Insights

Create an Application Insights resource in your Azure account. Copy the Instrumentation Key.
![](https://res.cloudinary.com/djwdcmwdz/image/upload/v1716403679/docs/portal.azure.com__quickstart_true_1_i1upqy.png)

Configure the `.env` like shown below.

```bash
# Get the required information here: https://app.tracetest.io/retrieve-token

TRACETEST_API_KEY="<YOUR_TRACETEST_API_KEY>"
TRACETEST_API_TOKEN="<YOUR_TRACETEST_TOKEN>"

# Azure
APP_INSIGHTS_INSTRUMENTATION_STRING="InstrumentationKey=14f0532a-aad4-4cb1-9bdb-64d021622efd;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/"
```

:::note
[Read the Azure Application Insights tracing backend configuration guide to learn more.](/configuration/connecting-to-data-stores/azure-app-insights)
:::

## Running the Tests

The [`docker-compose.yaml` file](https://github.com/kubeshop/tracetest/blob/main/examples/tracetest-azure-app-insights-collector/docker-compose.yaml) in the root directory contains the Node.js app, OpenTelemetry Collector and Tracetest Agent.

To run everything including Tracetest tests, run this command:

```bash
docker compose run tracetest-run
```

This will:
1. Start the Node.js app and send the traces to Azure App Insights.
2. Start the Tracetest Agent.
3. Configure the Azure App Insights tracing backend and create tests in your environment.
4. Run the tests.

### The Test File

Check out the `resources/test.yaml` file.

```yaml
# resources/test.yaml
type: Test
spec:
  id: W656Q0c4g
  name: Test API
  description: Test the App.
  trigger:
    type: http
    httpRequest:
      method: GET
      url: http://app:8080
      headers:
        - key: Content-Type
          value: application/json
  specs:
    - selector: span[tracetest.span.type="http" name="GET /"]
      assertions:
        - attr:http.status_code  =  200
        - attr:tracetest.span.duration  <  500ms
```

## Learn More

Please visit our [examples in GitHub](https://github.com/kubeshop/tracetest/tree/main/examples) and join our [Slack Community](https://dub.sh/tracetest-community) for more info!
